/*
 * Project Name spring-boot-tools
 * File Name MinaServerRunner
 * Package Name com.huxiaosu.tools.mina
 * Create Time 2024/5/25
 * Create by name：liujie
 */
package com.huxiaosu.tools.mina;

import com.huxiaosu.tools.common.executors.ExecutorsUtil;
import com.huxiaosu.tools.mina.action.MessageHandler;
import com.huxiaosu.tools.mina.codec.CodecFactory;
import com.huxiaosu.tools.mina.config.MinaConfig;
import com.huxiaosu.tools.mina.filter.ConnectionFilter;
import com.huxiaosu.tools.mina.filter.KeepAliveMessageFactoryImpl;
import com.huxiaosu.tools.mina.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.keepalive.KeepAliveFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filter.ssl.SslFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

/**
 * Description
 *
 * @author liujie
 * @date 2024/5/25 12:00
 */
@Slf4j
@Component
public class MinaServerRunner implements ApplicationRunner {
    @Autowired
    private MinaConfig minaConfig;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        IoAcceptor acceptor = new NioSocketAcceptor();
        // 添加 ssl 校验
        //设置加密过滤器
        //设置客户连接时需要验证客户端证书
        if(minaConfig.isEnableSsl()){
//            SslFilter sslFilter = new SslFilter(PushServerSslContextFactory.getInstance());
//            sslFilter.setNeedClientAuth(false);
//            acceptor.getFilterChain().addLast("sslFilter", sslFilter);
        }

        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        acceptor.getFilterChain().addLast("myfliter", new ConnectionFilter());
        // 指定编码过滤器
        acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CodecFactory(Constants.CHARSET)));
        acceptor.getFilterChain().addLast("threadPool", new ExecutorFilter(ExecutorsUtil.createExecutors()));
        //发送缓冲区10M
        acceptor.getSessionConfig().setReadBufferSize(2048 * 5000 * 10);
        //acceptor.getSessionConfig().set
        KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();
        KeepAliveFilter kaf = new KeepAliveFilter(kamfi, IdleStatus.BOTH_IDLE);
        // 是否回发
        kaf.setForwardEvent(true);
        acceptor.getFilterChain().addLast("heart", kaf);
        // 指定业务逻辑处理器
        acceptor.setHandler(new MessageHandler());
        // 设置端口号
        acceptor.setDefaultLocalAddress(new InetSocketAddress(minaConfig.getPort()));

        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, minaConfig.getHeartBeat());
        // 启动监听
        acceptor.bind();
        log.info("<server start success, port is [{} ] >" , minaConfig.getPort());
    }
}
